/**
 * Package:com.afteryuan.server.game.handler;
 * $Id$
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.server.game.handler;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.afteryuan.server.ConnectSession;
import com.afteryuan.server.command.up.CommandUp;
import com.afteryuan.server.data.Room;
import com.afteryuan.server.data.Sprite;
import com.afteryuan.server.data.SpritePool;
import com.afteryuan.server.game.command.down.SkillSingleDown;
import com.afteryuan.server.game.command.up.SkillSingleUp;
import com.afteryuan.server.handler.Handler;
import com.afteryuan.server.service.SpriteService;

/**
 * SkillSingleHandler
 * <p/>
 * 处理步骤:
 * 1.判断技能是否合法.
 * 2.如果合法,则广播给指定范围(目前为全空间广播)
 * <p><a href="SkillSingleHandler.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author$</a>
 * @version $Revision$
 */
public class SkillSingleHandler implements Handler {
    private static Log log = LogFactory.getLog(SkillSingleHandler.class);
    SpriteService spriteService;
    SpritePool spritePool;

    public void setSpritePool(SpritePool spritePool) {
		this.spritePool = spritePool;
	}
    
    public SkillSingleHandler() {
        log.debug("init SkillSingleHandler");
    }

    public void setSpriteService(SpriteService spriteService) {
        this.spriteService = spriteService;
    }

    public void process(ConnectSession session, CommandUp command) {
//    	Long begin = new Date().getTime();
        SkillSingleUp cmd = (SkillSingleUp) command;
        //todo 1.判断技能是否合法
        
        //2.如果合法,则广播给指定范围(目前为全空间广播)
        SkillSingleDown down = new SkillSingleDown();
        down.skillId = cmd.skillId;
        down.sourceId = cmd.sourceId;
        down.targetId = cmd.targetId;
        down.damage = 60;
        if("1".equals(cmd.skillId) || "2".equals(cmd.skillId)){
        	Sprite sp = spritePool.get(cmd.targetId);
        	if(sp != null){
        		sp.setHp(sp.getHp() - 60);        		
        	}
        }
        if("5".equals(cmd.skillId)){
        	Sprite sp = spritePool.get(cmd.targetId);
        	if(sp != null){
        		sp.setHp(sp.getHp() + 300);        		
        	}
        }
//        System.out.println("first:  " + (new Date().getTime() - begin) + " ms");
        spriteService.broadcast(new Room(cmd.sceneId,cmd.sceneInstanceId),down);
//        System.out.println("second: " + (new Date().getTime() - begin) + " ms");
    }
}